package com.transpot.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.transpot.entry.car.Car;
import com.transpot.entry.driver.Driver;
import com.transpot.entry.order.Agreement;
import com.transpot.entry.order.AgreementCustomerVO;
import com.transpot.entry.order.OrderBase;
import com.transpot.service.order.AgreementService;
import com.transpot.utils.R;
import com.transpot.utils.UploadUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 合同类controller层方法
 */
@RestController
@RequestMapping("/agreement")
public class AgreementController {
    @Autowired
    private AgreementService agreementService;

    /**
     * 添加和修改合同接口方法(有文件)
     * @param agreementJson
     * @param file
     * @return
     */
    @PostMapping("/addOrUpdateAgreement")
    public R addOrUpdateAgreement(@RequestParam("agreement") String agreementJson, @RequestParam("file") MultipartFile file) {
        //创建json转化对象
        ObjectMapper mapper = new ObjectMapper();
        //注册Java8的时间类型
        mapper.registerModule(new JavaTimeModule());
        //把json字符串转成对象
        Agreement agreement = null;
        try {
            agreement = mapper.readValue(agreementJson, Agreement.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //调用图片上传方法上传图片
        if (!file.isEmpty()) {
            //调用方法将图片上传，得到图片返回路径
            String imgUrl = UploadUtil.uploadImg(file);
            //将图片路径存入图片信息对象中
            agreement.setImgUrl(imgUrl);
        }
        //当主键为空时，则证明数据库没有该数据则为保存操作，否则为修改操作
        if (agreement.getId() == null) {
            //设置当前时间为创建时间
            agreement.setCreateTime(LocalDateTime.now());
            //设置当前时间为修改时间
            agreement.setUpdateTime(LocalDateTime.now());
            agreementService.save(agreement);
        } else {
            //设置当前时间为修改时间
            agreement.setUpdateTime(LocalDateTime.now());
            agreementService.updateById(agreement);
        }

        return R.success();
    }
    /**
     * 添加和修改合同接口方法（无文件）
     * @param agreementJson
     * @return
     */
    @PostMapping("/addOrUpdateAgreementNoFile")
    public R addOrUpdateAgreementNoFile(@RequestParam("agreement") String agreementJson) {
        //创建json转化对象
        ObjectMapper mapper = new ObjectMapper();
        //注册Java8的时间类型
        mapper.registerModule(new JavaTimeModule());
        //把json字符串转成对象
        Agreement agreement = null;
        try {
            agreement = mapper.readValue(agreementJson, Agreement.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //当主键为空时，则证明数据库没有该数据则为保存操作，否则为修改操作
        if (agreement.getId() == null) {
            //设置当前时间为创建时间
            agreement.setCreateTime(LocalDateTime.now());
            //设置当前时间为修改时间
            agreement.setUpdateTime(LocalDateTime.now());
            agreementService.save(agreement);
        } else {
            //设置当前时间为修改时间
            agreement.setUpdateTime(LocalDateTime.now());
            agreementService.updateById(agreement);
        }
        return R.success();
    }

    /**
     * 根据条件分页查询合同信息
     * @return
     */
    @GetMapping("/getAgreementByPageAndCondition")
    public R getAgreementByPageAndCondition(int current, int size,
                                            String firstParty, String secondParty, String title) {
        //创建分页查询对象
        IPage<Agreement> agreementIPage = new Page<>(current, size);
        //创建条件查询对象
        LambdaQueryWrapper<Agreement> agreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //当对应的查询条件不为空时，设置查询条件为甲方，乙方，合同标题，
        agreementLambdaQueryWrapper.eq(StringUtils.isNotBlank(firstParty), Agreement::getFirstParty, firstParty)
                .eq(StringUtils.isNotBlank(secondParty), Agreement::getSecondParty, secondParty)
                .eq(StringUtils.isNotBlank(title), Agreement::getTitle, title);
        //调用mp的原生分页查询方法进行查询
        agreementService.page(agreementIPage, agreementLambdaQueryWrapper);
        //返回查询结果
        return R.success(agreementIPage);
    }

    /**
     * 获取系统内全部合同
     * @return
     */
    @GetMapping("/getAllAgreement")
    public R getAllAgreement() {
        List<Agreement> agreementList = agreementService.list();
        return R.success(agreementList);
    }

    /**
     * 根据合同id去查找合同和客户信息
     * @param id
     * @return
     */
    @GetMapping("/getAgreementAndCustomerById")
    public R getAgreementAndCustomerById(Integer id) {
        //调用service层自定义方法实现该功能
        AgreementCustomerVO agreementCustomerVO = agreementService.getAgreementAndCustomer(id);
        return R.success(agreementCustomerVO);
    }

    /**
     * 根绝id删除合同信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAgreementById")
    public R deleteAgreementById(Integer id) {
        //调用service层的自定义方法处理删除
        boolean flag = agreementService.deleteAgreementById(id);
        //判断删除结果
        if (flag) {
            return R.success();
        } else {
            return R.fail();
        }
    }
}
